package com.example.administrator.managesystem.presenter.profile;

import android.content.Context;
import android.util.Log;
import android.widget.ImageView;

import com.example.administrator.managesystem.bean.CollectBean;
import com.example.administrator.managesystem.bean.LandListBean;
import com.example.administrator.managesystem.bean.MyCircleBean;
import com.example.administrator.managesystem.bean.RepostBean;
import com.example.administrator.managesystem.network.GuideAPI;
import com.example.administrator.managesystem.network.ServiceManager;
import com.example.administrator.managesystem.ui.MyCircleView;
import com.example.administrator.managesystem.utils.ToastUtils;

import java.io.IOException;

import okhttp3.ResponseBody;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class MyCirclePresenter {
    private MyCircleView myCircleView;

    public MyCirclePresenter(MyCircleView myCircleView) {
        this.myCircleView = myCircleView;
    }

    public void myCircleRequest(String user_id, String token, final int page, int pageSize , final int type, final Context context){
        ServiceManager.getInstance(context).getService(GuideAPI.class).myCircleRequest(user_id,token,page,pageSize)
                .subscribeOn(Schedulers.io())//IO线程加载数据
                .observeOn(AndroidSchedulers.mainThread())//主线程显示数据
                .subscribe(new Subscriber<MyCircleBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable t) {
                        ToastUtils.showToast(context,t.getMessage());
                    }

                    @Override
                    public void onNext(MyCircleBean myCircleBean) {
                        if(myCircleBean.getStatus()==200){
                            myCircleView.updateUI(myCircleBean.getData(),type);

                        }else{
                            ToastUtils.showToast(context,myCircleBean.getMsg());
                        }
                    }

                });


    }

    public void collectRequest(String user_id, String breeding_id, String token, final ImageView imageView, final Context context){
        ServiceManager.getInstance(context).getService(GuideAPI.class).CollectCircleRequest(user_id,breeding_id,token)
                .subscribeOn(Schedulers.io())//IO线程加载数据
                .observeOn(AndroidSchedulers.mainThread())//主线程显示数据
                .subscribe(new Subscriber<CollectBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable t) {
                        ToastUtils.showToast(context,t.getMessage());
                    }

                    @Override
                    public void onNext(CollectBean collectBean) {
                        if(collectBean.getStatus()==200){
                            ToastUtils.showToast(context,collectBean.getData());
                            myCircleView.showSuccess(imageView);
                        }
                    }

                });
    }
    public void repostRequest(String user_id, String breeding_id, final int type, String token, final Context context){
        ServiceManager.getInstance(context).getService(GuideAPI.class).shareCircleRequest(user_id,breeding_id,token)
                .subscribeOn(Schedulers.io())//IO线程加载数据
                .observeOn(AndroidSchedulers.mainThread())//主线程显示数据
                .subscribe(new Subscriber<RepostBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable t) {
                        ToastUtils.showToast(context,t.getMessage());
                    }

                    @Override
                    public void onNext(RepostBean repostBean) {
                        if(repostBean.getStatus()==200){
                       myCircleView.repost("",type);
                        }
                    }

                });
    }
}
